<?php


namespace App\Services;

use App\Exports\DocBookExport;
use App\Exports\LedgerTenderExport;

class ArchiveManageService extends BaseService
{
    /**
     * 获取供选择的项目
     *
     * @return array
     * @author: lxk
     * @date: 2024/5/10 10:29
     */
    public function getProjects($year, $agency, $storePeriod)
    {
        $chooseProjects = CommonService::getDocArchiveInstance()
            ->where('company_id', company_id())
            ->when($year, function($query) use ($year) {
                $query->where('year', $year);
            })
            ->when($agency, function($query) use ($agency) {
                $query->where('agency', $agency);
            })
            ->when($storePeriod, function($query) use ($storePeriod) {
                $query->where('store_period', $storePeriod);
            })
            ->pluck('project');
        $data = [];
        if ($chooseProjects->isNotEmpty()) {
            $chooseProjects = $chooseProjects->toArray();
            $chooseProjects = array_unique($chooseProjects);
            foreach ($chooseProjects as $project) {
                $tmp['value'] = $project;
                $tmp['label'] = $project;
                if ($project) {
                    $data[] = $tmp;
                }
            }
        }
        rsort($data);

        return $data;
    }

    /**
     * 获取供选择的年份
     *
     * @return array
     * @author: lxk
     * @date: 2024/5/10 10:29
     */
    public function getYears($project, $agency, $storePeriod)
    {
        $chooseYears = CommonService::getDocArchiveInstance()
            ->where('company_id', company_id())
            ->when($project, function($query) use ($project) {
                $query->where('project', $project);
            })
            ->when($agency, function($query) use ($agency) {
                $query->where('agency', $agency);
            })
            ->when($storePeriod, function($query) use ($storePeriod) {
                $query->where('store_period', $storePeriod);
            })
            ->pluck('year');
        $data = [];
        if ($chooseYears->isNotEmpty()) {
            $chooseYears = $chooseYears->toArray();
            $chooseYears = array_unique($chooseYears);
            foreach ($chooseYears as $year) {
                $tmp['value'] = $year;
                $tmp['label'] = $year;
                if ($year) {
                    $data[] = $tmp;
                }
            }
        }
        rsort($data);

        return $data;
    }

    /**
     * 获取供选择的机构
     *
     * @return array
     * @author: lxk
     * @date: 2024/5/10 10:29
     */
    public function getAgencies($project, $year, $storePeriod)
    {
        $chooseAgencies = CommonService::getDocArchiveInstance()
            ->where('company_id', company_id())
            ->when($project, function($query) use ($project) {
                $query->where('project', $project);
            })
            ->when($year, function($query) use ($year) {
                $query->where('year', $year);
            })
            ->when($storePeriod, function($query) use ($storePeriod) {
                $query->where('store_period', $storePeriod);
            })
            ->pluck('agency');
        $data = [];
        if ($chooseAgencies->isNotEmpty()) {
            $chooseAgencies = $chooseAgencies->toArray();
            $chooseAgencies = array_unique($chooseAgencies);
            foreach ($chooseAgencies as $agency) {
                $tmp['value'] = $agency;
                $tmp['label'] = $agency;
                if ($agency) {
                    $data[] = $tmp;
                }
            }
        }
        rsort($data);

        return $data;
    }

    /**
     * 获取供选择的保管期限
     *
     * @return array
     * @author: lxk
     * @date: 2024/5/10 10:29
     */
    public function getStorePeriods($project, $year, $agency)
    {
        $chooseStorePeriods = CommonService::getDocArchiveInstance()
            ->where('company_id', company_id())
            ->when($project, function($query) use ($project) {
                $query->where('project', $project);
            })
            ->when($year, function($query) use ($year) {
                $query->where('year', $year);
            })
            ->when($agency, function($query) use ($agency) {
                $query->where('agency', $agency);
            })
            ->pluck('store_period');
        $data = [];
        if ($chooseStorePeriods->isNotEmpty()) {
            $chooseStorePeriods = $chooseStorePeriods->toArray();
            $chooseStorePeriods = array_unique($chooseStorePeriods);
            foreach ($chooseStorePeriods as $storePeriod) {
                $tmp['value'] = $storePeriod;
                $tmp['label'] = $storePeriod;
                if ($storePeriod) {
                    $data[] = $tmp;
                }
            }
        }
        rsort($data);

        return $data;
    }

    /**
     * 获取文书档案
     * @author: lxk
     * @date: 2024/5/8 17:18
     */
    public function getDocArchives($project, $year, $agency, $storePeriod, $keyword, $perPage)
    {
        $docArchive = CommonService::getDocArchiveInstance();
        $list = $docArchive->where('company_id', company_id())
            ->when($project, function ($query) use ($project) {
                $query->where('project', $project);
            })
            ->when($year, function ($query) use ($year) {
                $query->where('year', $year);
            })
            ->when($agency, function ($query) use ($agency) {
                $query->where('agency', $agency);
            })
            ->when($storePeriod, function ($query) use ($storePeriod) {
                $query->where('store_period', $storePeriod);
            })
            ->when($keyword, function ($query) use ($keyword) {
                $query->where('title', 'like', "%{$keyword}%");
            })
            ->whereNull('deleted_at')
            ->orderBy('id')
            ->paginate($perPage); //按照序号从小到大;

        $list = $list->toArray();
        $page = request()->input('page', 1);
        foreach ($list['data'] as $key=>&$val) {
            $val['number']  = ($page - 1) * $perPage + $key + 1;
        }

        return $this->removeNoUsedPageParams($list);
    }

    /**
     * 修改文书档案
     *
     * @param $params
     * @return bool
     * @author: lxk
     * @date: 2025-02-19 9:53
     */
    public function updateData($params) : bool
    {
        $docArchive            = CommonService::getDocArchiveInstance()->find($params['id']);
        $docArchive->year      = format_str($params['year']);
        $docArchive->agency    = format_str($params['agency']);
        $docArchive->box_num   = format_str($params['box_num']);
        $docArchive->piece_num = format_str($params['piece_num']);
        $docArchive->director  = format_str($params['director']);
        $docArchive->file_num  = format_str($params['file_num']);
        $docArchive->title     = format_str($params['title']);
        $docArchive->c_date    = format_str($params['c_date']);
        $docArchive->page_num  = format_str($params['page_num']);
        $docArchive->remark    = format_str($params['remark']);
        if ($docArchive->save()) {
            return true;
        }
        return false;
    }


    /**
     * 导出文书档案
     *
     * @param $project
     * @param $year
     * @param $agency
     * @param $storePeriod
     * @param $keyword
     * @return array
     * @author: lxk
     * @date: 2025-02-19 11:23
     */
    public function export($project, $year, $agency, $storePeriod, $keyword) : array
    {
        $prefix = "";
        if ($project)     $prefix = "{$project}";
        if ($year)        $prefix = "{$prefix}_{$year}年";
        if ($agency)      $prefix = "{$prefix}_{$agency}";
        if ($storePeriod) $prefix = "{$prefix}_{$storePeriod}";
        if ($keyword)     $prefix = "{$prefix}_{$keyword}";
        $filePath = "public/excels/{$prefix}_文书档案_" . date('His') . '.xlsx';
        (new DocBookExport($project, $year, $agency, $storePeriod, $keyword))->store($filePath);
        $filePath    = str_replace('public', 'storage', $filePath);
        $downloadUrl = config('site.domain') . $filePath;
        $fileInfo    = pathinfo($downloadUrl);
        CommonService::addActionLog("导出文书档案");

        return [
            'basename'     => $fileInfo['basename'],
            'download_url' => $downloadUrl
        ];
    }

}
